//
//  CustomFont.swift
//  Swift-30-demo
//
//  Created by apple on 2021/11/22.
//

import UIKit

class CustomFont: UIViewController {
    let label = UILabel()
    let newLabel = UILabel()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "CustomFont"
        // Do any additional setup after loading the view.
        self.view.backgroundColor = UIColor.white
        
        createMainView()
    }
    
    func createMainView() {
        label.text = "I am the king of the world"
        label.textAlignment = NSTextAlignment.center
        self.view.addSubview(label)
        label.snp.makeConstraints { make in
            make.top.equalTo(navgationHeight + 50)
            make.centerX.equalTo(self.view)
        }
        label.font = UIFont.systemFont(ofSize: 30)
        
        newLabel.text = "welcome to the world"
        newLabel.textAlignment = NSTextAlignment.center
        self.view.addSubview(newLabel)
        newLabel.snp.makeConstraints { make in
            make.top.equalTo(label.snp.bottom).offset(100)
            make.centerX.equalTo(self.view)
        }
        newLabel.font = UIFont(name: "Savoye LET", size: 30)
        
        let changeBtn = UIButton(type: .custom)
        changeBtn.setTitle("Change Font Family", for: .normal)
        changeBtn.addTarget(self, action: #selector(changeFontFamily), for: .touchUpInside)
        changeBtn.setTitleColor(UIColor.blue, for: .normal)
        self.view.addSubview(changeBtn)
        changeBtn.layer.borderColor = UIColor.blue.cgColor
        changeBtn.layer.borderWidth = 1
        changeBtn.layer.cornerRadius = 5
        changeBtn.snp.makeConstraints { (make) in
            make.top.equalTo(500)
            make.centerX.equalTo(self.view)
            make.width.equalTo(200)
        }
        
        // 获取所有字体名称
        printAllSupportedFontNames()
    }

    @objc func changeFontFamily(sender: UIButton) {
        sender.isSelected = !sender.isSelected
        if sender.isSelected {
            label.font = UIFont(name: "Savoye LET", size: 30)
            newLabel.font = UIFont.systemFont(ofSize: 30)
        }else {
            label.font = UIFont.systemFont(ofSize: 30)
            newLabel.font = UIFont(name: "Savoye LET", size: 30)
        }
    }
    
    func printAllSupportedFontNames() {
        let familyNames = UIFont.familyNames
        for familyName in familyNames {
            print("++++++ \(familyName)")
//            let fontNames = UIFont.fontNames(forFamilyName: familyName)
//            for fontName in fontNames {
//                print("----- \(fontName)")
//            }
        }
    }
}
